﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Web;
using System.Text.RegularExpressions;
using System.Threading;
using System.Collections.Generic;

namespace FeedSync
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void testRSSReader()
        {
            textBox1.Text = string.Empty;

            RssReader rssReader = new RssReader("http://www.caobian.info/?feed=rss");
            rssReader.Execute();
            foreach (var item in rssReader.Items)
            {
                textBox1.Text += item.Title + "\r\n";
            }

        }

        private string PostFanfou(string username, string password, string msg)
        {
            HttpHelper httpHelper = new HttpHelper();
            httpHelper.SetCredentials(username, password);

            string postData = "status=" + HttpUtility.UrlEncode(msg);
            return httpHelper.GetHtml("http://api.fanfou.com/statuses/update.xml", postData, true);
        }

        private string PostJiwai(string username, string password, string msg)
        {
            HttpHelper httpHelper = new HttpHelper();
            httpHelper.SetCredentials(username, password);

            string postData = "status=" + HttpUtility.UrlEncode(msg);
            return httpHelper.GetHtml("http://api.jiwai.de/statuses/update.json", postData, true);
        }

        private string PostDigu(string username, string password, string msg)
        {
            HttpHelper httpHelper = new HttpHelper();
            httpHelper.SetCredentials(username, password);

            string postData = "content=" + HttpUtility.UrlEncode(msg) + "";
            return httpHelper.GetHtml("http://api.digu.com/statuses/update.json", postData, true);
        }

        private string PostTwitter(string username, string password, string msg)
        {
            HttpHelper httpHelper = new HttpHelper();
            httpHelper.SetCredentials(username, password);

            string postData = "status=" + HttpUtility.UrlEncode(msg) + "";
            return httpHelper.GetHtml("https://twitter.com/statuses/update.xml", postData, true);
        }

        private string tinyurl(string url)
        {
            HttpHelper httpHelper = new HttpHelper();
            string fullurl = "http://tinyurl.com/api-create.php?url=" + HttpUtility.UrlEncode(url);

            return httpHelper.GetHtml(fullurl);
        }

        private string stripHTMLtoTxt(string html)
        {
//            Regex reHtmlTag = new Regex(@"<.*?>");
            Regex reSpaces = new Regex(@"\s+");

            return reSpaces.Replace(html, " ");
//            return reSpaces.Replace(reHtmlTag.Replace(html, string.Empty), " ");
        }

        private string appendText(string source, string appendix, int maxlen)
        {
            // 如果appendix超长，用 ... 作为省略符。所以需要额外的3个字符。
            if (source.Length >= (maxlen - 3)) return source;

            if (source.Length + appendix.Length <= maxlen )
            {
                return source + appendix;
            }

            return source + appendix.Substring(0, maxlen - 3 - source.Length) + "...";
        }

        private string postItem(IList<IMiniBlog> miniBlogs, RssItem item)
        {
            string url = tinyurl(item.Link);
            string title = item.Title;
            string desc = stripHTMLtoTxt(item.Description);

            string msg = string.Empty;
            int maxLen = 140 - (url.Length + 1);
            msg = appendText(msg, title + ": ", maxLen);
            msg = appendText(msg, desc, maxLen);
            msg += " " + url;

            foreach (var miniBlog in miniBlogs)
            {
                miniBlog.post(msg);
            }

            Debug.WriteLine("msg 长度: " + Encoding.Default.GetBytes(msg).Length.ToString());
            return msg;
        }

        private void syncAll(string rss, IList<IMiniBlog> miniBlogs)
        {
            RssReader rssReader = new RssReader(rss);
            rssReader.Execute();

            int itemCount = rssReader.Items.Count;
            for (int i = itemCount - 1; i >= 0; i--)
            {
                postItem(miniBlogs, rssReader.Items[i]);
//                Thread.Sleep(4);
            }
        }

        private void syncLast(string rss, IList<IMiniBlog> miniBlogs)
        {
            RssReader rssReader = new RssReader(rss);
            rssReader.Execute();

            if (rssReader.Items.Count > 0) postItem(miniBlogs, rssReader.Items[0]);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string rss = "http://www.adwords8.net/feed/rss";
            string username = "*****", password = "*****";

            IList<IMiniBlog> miniBlogs = new List<IMiniBlog>();
            miniBlogs.Add(new Fanfou(username, password));
            miniBlogs.Add(new Jiwai(username, password));
            miniBlogs.Add(new Digu(username, password));
            miniBlogs.Add(new Twitter("adwords8", password));

            syncLast(rss, miniBlogs);

//            syncAll(rss, miniBlogs);
        }
    }
}
